package com.ant.storage.config;

import com.ant.storage.pojo.DepreciationDetails;
import com.ant.storage.pojo.HouseLease;
import com.ant.storage.pojo.StorageContract;
import com.ant.storage.service.IDepreciationService;
import com.ant.storage.service.IHouseLeaseService;
import com.ant.storage.service.IStorageContractService;
import com.ant.storage.utils.DateFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hzd
 * @version 1.0
 * @date 2023/11/25 9:33
 */
@Component
public class Scheduler {
    private static final Logger logger = LoggerFactory.getLogger(Scheduler.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Resource
    private IHouseLeaseService leaseService;

    @Autowired
    private IDepreciationService depreciationService;

    @Autowired
    private IStorageContractService storageContractService;

    /**
     * 每天2：00执行
     */
     @Scheduled(cron = "0 0 2 * * ?")
     //@Scheduled(cron = "*/10 * * * * ?")
    public void tokenTasks() {
        try {
            List<HouseLease> list = leaseService.list(new QueryWrapper<HouseLease>().eq("contract_status", 1));
            list.stream().forEach(item -> {
                Date deadline = item.getDeadline(); // 合同到期日期
                if (deadline!= null) {
                    boolean b = DateFormat.getDateFormat(deadline, new Date());
                    if (b) {
                        // 到期，更新状态
                        leaseService.update(new UpdateWrapper<HouseLease>().set("contract_status", 0)
                                .eq("id", item.getId()));
                    }
                }
            });
            logger.info("房屋合同定时任务执行时间：" + dateFormat.format(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 每个月1号的 1：00执行
     */
    @Scheduled(cron = "0 0 1 2 * *")
    public void depreciationTasks() {
        try {
            List<DepreciationDetails> carInfoList = depreciationService.findCarInfoList();
            depreciationService.saveDepreciation(carInfoList, 1);
            List<DepreciationDetails> detailsList = depreciationService.findDepreciationList();
            depreciationService.saveDepreciation(detailsList, 2);
            logger.info("折旧定时任务执行时间：" + dateFormat.format(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     *
     * 每天2：00执行
     *
     */
    //cron = "0 */1 * * * ?" 每1分钟执行一次
    @Scheduled(cron = "0 0 2 * * ?")
    public void storageContractTasks() {
        try {
            List<StorageContract> list = storageContractService.list(new QueryWrapper<StorageContract>()
                    .eq("is_recycle", 0));
            list.stream().forEach(item -> {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse(item.getContractEnd());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Date deadline = date; // 合同到期日期
                if (deadline!= null) {
                    boolean b = DateFormat.getDateFormat(deadline, new Date());
                    if (b) {
                        // 到期，更新状态
                        storageContractService.update(new UpdateWrapper<StorageContract>().set("is_recycle", 1).eq("id", item.getId()));
                    }
                }
            });
            logger.info("仓储合同定时任务执行时间：" + dateFormat.format(new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 结存流水
     */
   // @Scheduled(cron = "0 0 1 1 * *")
//    public void depreciationTasks() {
//        try {
//            List<DepreciationDetails> carInfoList = depreciationService.findCarInfoList();
//            depreciationService.saveDepreciation(carInfoList, 1);
//            List<DepreciationDetails> detailsList = depreciationService.findDepreciationList();
//            depreciationService.saveDepreciation(detailsList, 2);
//            logger.info("折旧定时任务执行时间：" + dateFormat.format(new Date()));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    //Sat Nov 16 00:00:00 CST 2024
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        System.out.println(time);
    }

}
